/*
  自定义dialog
  集成Dialog类，重写build函数
 */

import 'dart:async';

import 'package:flutter/material.dart';


class MyDialog extends Dialog {
  final String title;
  final String content;

  MyDialog({this.title='', this.content=''});

  // 定时器
  void _startTimer(context) {
    // 创建一个重复的定时器，所以一定要取消
    Timer.periodic(Duration(milliseconds: 2000), (timer) {
      Navigator.pop(context);
      timer.cancel();
    });
  }

  @override
  Widget build(BuildContext context) {
    _startTimer(context);
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          width: 200,
          height: 200,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(8.0))
          ),
          padding: EdgeInsets.all(8.0),
          // 这里就可以自定义布局了
          child: Text(this.title),
        ),
      ),
    );
  }
}

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  void _showDialog() async {
    var result = await showDialog(context: context, builder: (context) {
      return MyDialog(title: '哇哈哈',);
    });
    print('result is $result');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('自定义Dialog'),
      ),
      body: Center(
        child: TextButton(
          child: Text('显示弹窗'),
          onPressed: _showDialog,
        ),
      ),
    );
  }
}

